package com.magic.himalaya.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.magic.himalaya.R;
import com.magic.himalaya.utils.LogUtil;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @create: Create by Magic on 2022/1/24.
 */
public class TrackImagePagerAdapter extends PagerAdapter {
    private static final String TAG = TrackImagePagerAdapter.class.getSimpleName();

    private List<Track> mTrackList = new ArrayList<>();

    @Override
    public int getCount() {
        if (mTrackList != null)
            return mTrackList.size();
        return 0;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View itemView = LayoutInflater.from(container.getContext()).inflate(R.layout.item_track_pager, container, false);
        container.addView(itemView);
        ImageView imageView = itemView.findViewById(R.id.track_large_image);
        if (mTrackList.get(position).getCoverUrlLarge() != null && !mTrackList.get(position).getCoverUrlLarge().trim().equals(""))
            Picasso.get().load(mTrackList.get(position).getCoverUrlLarge()).into(imageView);
        return itemView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);//!!!!!!!重点
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;//!!!!!!!!重点
    }


    /**
     * 设置数据后进行重新渲染
     *
     * @param trackList
     */
    public void setData(List<Track> trackList) {
        this.mTrackList.clear();
        this.mTrackList.addAll(trackList);
        notifyDataSetChanged();
    }
}
